﻿using System;
using fastCSharp;

namespace diantou.web.answer
{
    /// <summary>
    /// 回答视图
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    class view<viewType> : web.view<viewType>
        where viewType : view<viewType>
    {
        /// <summary>
        /// 问题
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.question question;
        /// <summary>
        /// 用户问题关系
        /// </summary>
        protected question.userQuestion userQuestion
        {
            get { return new question.userQuestion { Question = question, UserId = currentUserId }; }
        }
        /// <summary>
        /// 回答
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.answer answer;
        /// <summary>
        /// 回答列表
        /// </summary>
        private userAnswer[] answerArray = new userAnswer[1];
        /// <summary>
        /// 回答列表
        /// </summary>
        protected userAnswer[] answers
        {
            get
            {
                answerArray[0] = new userAnswer { Answer = answer, UserId = currentUserId };
                return answerArray;
            }
        }
        /// <summary>
        /// 回答的用户
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.user user;
        /// <summary>
        /// 相关收藏top5
        /// </summary>
        protected userAnswer.relateFavorite relateFavorite
        {
            get
            {
                return new userAnswer.relateFavorite { ViewOnly = requestHeader.IsReView, Answer = new userAnswer { Answer = answer, UserId = currentUserId } };
            }
        }
        /// <summary>
        /// 是否显示邀请回答
        /// </summary>
        protected bool isQuestionInvite
        {
            get { return false; }
        }        /// <summary>
        /// 是否显示答案连接
        /// </summary>
        protected bool isAnswerPath
        {
            get { return true; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int answerId)
        {
            return load(answerId);
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="searchEnginePath">搜索引擎重定向</param>
        /// <returns></returns>
        protected bool load(int answerId, string searchEnginePath = null)
        {
            if (answerId == 0)
            {
                checkSearchEngine(searchEnginePath, webConfig.NotFound404);
                return false;
            }
            answer = diantou.dataAccess.tcpCall.answer.Get(answerId);
            if (answer != null)
            {
                user = answer.Remote.User;
                question = answer.Remote.Question;
                if (user == null) user = diantou.dataAccess.user.NullUser;
                return true;
            }
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
        /// <summary>
        /// 清除数据
        /// </summary>
        protected override void clear()
        {
            answerArray[0].Answer = null;
            base.clear();
        }
    }
}
